# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate:
# Message:

import requests
import os

def parse_url(self, url, method, post_data=None, headers=None, tofile=True, **kwargs):
    requests.packages.urllib3.disable_warnings()  # 因为把ssl安全验证关了,所以不让代码报不安全的https请求警告
    pid = kwargs.get("local_pid", "")
    charset = kwargs.get("charset", "utf8")  # 暂时没有用到
    if method == 'GET':
        data = "GET" + url + pid  # 拼接data
        md5 = self.getmd5(data)  # 获取此次请求电子指纹
        file_path = self.base_path + md5 + ".html"
        if os.path.exists(file_path):  # 判断文件是否存在,如果存在就从本地读取
            print "GET for local"
            with open(file_path, 'r') as f:
                res = f.read()
            response = res
        else:  # 如果路径不存在, 从web端请求
            print "GET for web"
            try:
                response = requests.get(url=url, verify=False, headers=headers, cookies=self.cookies, timeout=30,
                                        allow_redirects=False)
            except Exception as e:
                response = requests.get(url=url, verify=False, headers=headers, cookies=self.cookies, timeout=30,
                                        allow_redirects=False)

            # 获取cookies, 并更新self.cookies
            cookies = response.cookies.get_dict()
            for k in cookies:
                self.cookies[k] = cookies[k]

            # 获取location,
            location = response.headers.get("Location", None)
            if location:
                print "响应头中存在location"
                self.location = location
                # return response.content.decode('utf8')

            if response:
                if tofile:
                    with open(file_path, 'w') as f:  # 保存此次请求的html
                        f.write(response.content.decode('utf8'))
                response = response.content.decode("utf8")
            else:
                response = None

    elif method == "POST":
        data = "POST" + url + json.dumps(post_data)
        md5 = self.getmd5(data)
        file_path = self.base_path + md5 + ".html"
        if os.path.exists(file_path):  # 判断文件是否存在,如果存在就从本地读取
            print "POST for local"
            with open(file_path, 'r') as f:
                res = f.read()
            response = res
        else:
            print "POST for web"
            try:
                response = requests.post(url=url, data=post_data, verify=False, headers=headers,
                                         cookies=self.cookies, timeout=30,
                                         allow_redirects=False)
            except Exception as e:
                response = requests.post(url=url, data=post_data, verify=False, headers=headers,
                                         cookies=self.cookies, timeout=30,
                                         allow_redirects=False)

            # 获取cookies, 并更新self.cookies
            cookies = response.cookies.get_dict()
            for k in cookies:
                self.cookies[k] = cookies[k]

            # 获取location,
            location = response.headers.get("Location", None)
            if location:
                print "响应头中存在location"
                self.location = location
                # return response.content.decode('utf8')

            if response:
                if tofile:
                    with open(file_path, 'w') as f:  # 保存此次请求的html
                        f.write(response.content.decode('utf8'))
                response = response.content.decode("utf8")

            else:
                response = None

    return response


# b = 'aaa' + None